import { Injectable } from "@angular/core";
import { BaseHttpService } from "../../../shared/services/baseHttp.service";
import { HttpClient, HttpParams } from "@angular/common/http";
import { Observable } from "rxjs";
import { InsightsModel } from "./model/insights.model";
import { InsightsNamesModel } from "./model/insightsNames.model";

@Injectable()
export class InsightsService extends BaseHttpService {

    constructor(private http: HttpClient) {
        super()
    }

    public getInsightsList(term: string, topicId: string, sectorId: string, skip: number, take: number): Observable<InsightsModel[]> {
        let params = new HttpParams();
        params = params.set('titlePart', term)
            .set('fromAdmin', true.toString())
            .set('skip', skip.toString())
            .set('take', take.toString())
            .set('orderBy', 'DatePublished')
            .set('orderAscending', false.toString())
        if(sectorId !== undefined)
            params = params.set('insightSectorId', sectorId);
        if(topicId !== undefined)
            params = params.set('insightTopicId', topicId);

        return this.http.get<InsightsModel[]>(this.serverUrl + '/insightsserv/api/v1/insights', { params: params });
    }

    public getInsightsCount(term: string, topicId: string, sectorId: string): Observable<number> {
        let params = new HttpParams();
        params = params.set('titlePart', term)
            .set('getcount', true.toString())
            .set('fromAdmin', true.toString())
            .set('insightSectorId', sectorId)
            .set('insightTopicId', topicId);

        return this.http.get<any>(this.serverUrl + '/insightsserv/api/v1/insights', { params: params }).map(x => x.count);
    }

    public getInsightsNames(): Observable<InsightsNamesModel> {
        return this.http.get<InsightsNamesModel>(this.serverUrl + '/insightsserv/api/v1/insightsnames');
    }

    public publishInsights(insights: any[]): Observable<void> {
        return this.http.patch<void>(this.serverUrl + '/insightsserv/api/v1/insights', insights);
    }

    public deleteInsights(insights: any[]): Observable<void> {
        return this.http.patch<void>(this.serverUrl + '/insightsserv/api/v1/insights', insights);
    }

    public getInsight(id: string): Observable<InsightsModel> {
        let params = new HttpParams();
        params = params.set('fromAdmin', true.toString())
            .set('includeAllFields', true.toString());
        return this.http.get<InsightsModel>(this.serverUrl + '/insightsserv/api/v1/insights/' + id, { params: params });
    }

    public publishInsight(insight: InsightsModel): Observable<InsightsModel>{
        return this.http.post<InsightsModel>(this.serverUrl + '/insightsserv/api/v1/insights', insight);
    }
    
    public saveInsight(insight: InsightsModel): Observable<InsightsModel>{
        return this.http.put<InsightsModel>(this.serverUrl + '/insightsserv/api/v1/insights', insight);
    }

    public getUploadUrl(): string {
        return this.serverUrl + '/insightsserv/api/v1/files/';
    }
}